
const requestMode = require('../../../js/requestsMode.js');
const session = require('../../../js/session.js');
var Tools = require('../../../js/Tools.js');
Page({
    data:{
        focus:false,
        modalStyle:"display:none;left:100%;",
        addMoney:'',
        hasMoney:'',
        placeholder:'填写充值金额',
        addOrGet : '充值',
        settingMoney:'1'
    },
    seeConsumerDetailed:function(){
        wx.navigateTo({
            url: '../consumerDetailed/consumerDetailed'
        })
    },
    settingMoney:function(e){
        let type = e.currentTarget.dataset.type;
        let This = this;
        if(type == 1){
            This.setData({
                modalStyle:"translation:all 0.2s;left:0;display:flex",
                focus:true,
                addOrGet : '充值',
                placeholder:'充值金额',
                settingMoney:type
            })
        }else{
            This.setData({
                modalStyle:"translation:all 0.2s;left:0;display:flex",
                focus:true,
                addOrGet : '提现',
                placeholder:'提现金额',
                settingMoney:type
            })
        }
    },
    cancel:function(){
        let This = this;
        This.setData({
            modalStyle:"translation:all 0.2s;display:none;left:100%;",
            focus:false,
            addMoney:''
        })
    },
    errorModal:function(){
        Tools.showWrongNetWorkToast();
    },
    messageError:function(data){
        Tools.showErrorToast(data);
    },
    onLoad:function(){
        let This = this;
        requestMode.sendRequest('member/index','POST',{},{sKey:session.get('sKey')}).then(function(res){
            let code = res.data.code;
            if(code == 200){
                let data = res.data.data;
                This.setData({
                    hasMoney:data.member_capital
                })
            }else{
                Tools.showErrorToast("支付失败");
            }
        },function(res){
            Tools.showErrorToast("支付失败");
        })
    },
    sure:function(){
        let This = this;
        let floatAddMoney = parseFloat(This.data.addMoney);
        let settingMoney = This.data.settingMoney;
        let hasMoney = This.data.hasMoney;
        let GetMoney = This.data.addMoney;
        if(floatAddMoney){
            if(settingMoney == 1){
                //充值
                requestMode.sendRequest('member/recharge','POST',{total_fee:This.data.addMoney},{sKey:session.get('sKey')}).then(function(res){
                    let getData = res.data.data;
                    let code = res.data.code;
                    if(code == 200){
                        wx.requestPayment({
                            appId:getData.appId,
                            'timeStamp': getData.timeStamp,
                            'nonceStr':getData.nonceStr,
                            'package': getData.package,    //  支付id
                            'signType':getData.signType,
                            'paySign': getData.paySign,
                            success:function(res){
                                This.cancel();
                                This.onLoad();
                            },
                            fail:function(res){
                                Tools.showErrorToast("支付失败！");
                                This.cancel();
                            }
                        })
                    }else{
                        Tools.showErrorToast("支付失败");
                        This.cancel();
                    }
                },function(){
                    Tools.showErrorToast("支付失败");
                    This.cancel();
                });
            }else{
                //提现
                requestMode.sendRequest('member/withdrawals','POST',{total_fee : This.data.addMoney},{sKey:session.get('sKey')}).then(function (getMoneySuccess) {
                    This.cancel();
                    let code = getMoneySuccess.data.code;
                    if(code == 200){
                        This.setData({
                            hasMoney: (parseFloat(hasMoney)-parseFloat(GetMoney)).toFixed(2)
                        });
                        Tools.showSuccessInfoToast('提现成功');
                    }else{
                        Tools.showErrorToast('提现失败');
                    }
                }, function (getMoneyFail) {
                    Tools.showErrorToast('提现失败');
                    This.cancel();
                })
            }
        }else{
            This.setData({
                addMoney:'',
                placeholder:'请输入正确的金额格式'
            });
            if(settingMoney == 1){
                setTimeout(function(){
                    This.setData({
                        placeholder:'充值金额'
                    });
                },2000)
            }else{
                setTimeout(function(){
                    This.setData({
                        placeholder:'填写提现金额'
                    });
                },2000)
            }
        }
    },
    moneyChange:function(e){
        this.setData({
            addMoney: e.detail.value
        })
    }
});
